package com.iculture;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Activity_DataStructure {
	public String UID; // 活動識別碼
	public String title; // 活動標題
	public String showUnit; // 演出單位
	public String time; // 演出時間
	//public String startDate; // 活動開始時間
	//public String endDate; // 活動結束時間
	public String location; //活動地點地址
	public String locationName; //活動地點名稱
	public String descriptionFilterHtml; // 簡介說明
	public String imageUrl; // 代表圖像
	public String latitude; //地點經度
	public String longitude; //地點緯度
	public String sourceWebPromote; // 推廣網址
	public String onSales; //是否售票
	public String webSales; // 售票網址
	public String startEndTime;//起訖日期
	// public String price; //售票說明
	// public String discountInfo; // 折價資訊
	// public String masterUnit; // 主辦單位
	// public String subUnit; // 協辦單位
	// public String supportUnit; // 贊助單位
	// public String otherUnit; // 其他單位
	// public String comment; // 備註
	// public int total; // 總筆數

	public ArrayList<Activity_DataStructure> parseJsonString(String json) {
		ArrayList<Activity_DataStructure> DataList = new ArrayList<Activity_DataStructure>();
		try {
			JSONArray array = new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				JSONObject j = array.getJSONObject(i);
				Activity_DataStructure d = new Activity_DataStructure();
				d.UID = j.optString("UID", "");
				d.title = j.optString("title", "");
				d.showUnit = j.optString("showUnit", "");
				d.time = j.optString("time", "");
				d.location = j.optString("location", "");
				d.locationName = j.optString("locationName", "");
				d.descriptionFilterHtml = j.optString("descriptionFilterHtml","");
				d.imageUrl = j.optString("imageUrl", "");						
				d.latitude = j.optString("latitude", "");
				d.longitude = j.optString("longitude", "");
				d.sourceWebPromote = j.optString("sourceWebPromote", "");
				d.onSales = j.optString("onSales", "");
				d.webSales = j.optString("webSales", "");
				d.startEndTime = j.optString("startEndTime", "");
				
				DataList.add(d);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return DataList;

	}

	public String getImage(String name) throws IOException{
		String imgURL="";
		String url="http://cloud.culture.tw/frontsite/inquiry/queryAction.do?method=doSearch2&keyword="+java.net.URLEncoder.encode(name);
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		HttpResponse response = 
		httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		
		BufferedReader reader = new BufferedReader(new 
				InputStreamReader(entity.getContent(), "UTF-8"),8192);
				String result = "";
				String line = null;
				while ((line = reader.readLine())!= null){
				result += line + "\n";
				}
				reader.close();
		try {
			JSONObject obj=new JSONObject(result);
			JSONArray array = obj.getJSONArray("data");
			JSONObject obj2 = array.getJSONObject(0);
			imgURL=obj2.optString("imageUrl");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (imgURL.contains("no-120.gif"))?"":"http://cloud.culture.tw"+imgURL;
    }

}
